package ar.com.spsolutions.splibrary.services.dto;

import java.util.Iterator;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import ar.com.spsolutions.splibrary.services.RoleService;
import ar.com.spsolutions.splibrary.services.UserService;
import ar.com.spsolutions.splibrary.services.exception.ServiceException;
import ar.com.spsolutions.splibrary.user.User;
import ar.com.spsolutions.splibrary.user.role.UserRole;

public class CRUDUserDTO extends AbstractCRUDDTO<User> {

	private static final long serialVersionUID = 1783211310681870394L;

	private static final String EMAIL = "email";

	private static final String PASSWORD = "password";

	private static final String ROLE = "role";

	private static final String NAME = "name";

	private String passwordConfirmation;

	public UserService getUserService() {
		return this.getLibraryServiceFacade().getEntityService()
				.getUserService();
	}

	// -- dto accesors --

	public List<? extends UserRole> roles() {
		return UserRole.roles();
	}

	public void setRole(final UserRole role) {
		this.setPropertyValue(CRUDUserDTO.ROLE, role);
	}

	public UserRole getRole() {
		return this.getPropertyValue(CRUDUserDTO.ROLE);
	}

	public void setEmail(final String email) {
		this.setPropertyValue(CRUDUserDTO.EMAIL, email);
	}

	public void setName(final String name) {
		this.setPropertyValue(CRUDUserDTO.NAME, name);
	}

	public String getEmail() {
		return this.getPropertyValue(CRUDUserDTO.EMAIL);
	}

	public String getName() {
		return this.getPropertyValue(CRUDUserDTO.NAME);
	}

	public void setPassword(final String password) {
		this.setPropertyValue(CRUDUserDTO.PASSWORD, password);
	}

	public String getPassword() {
		return this.getPropertyValue(CRUDUserDTO.PASSWORD);
	}

	public void setPasswordConfirmation(final String password) {
		this.passwordConfirmation = password;
	}

	public String getPasswordConfirmation() {
		if (this.passwordConfirmation == null) {
			return this.getPropertyValue(CRUDUserDTO.PASSWORD);
		}
		return this.passwordConfirmation;
	}

	public RoleService getRoleService() {
		return this.getLibraryServiceFacade().getEntityService()
				.getRoleService();
	}

	@Override
	public String getRenderPropertyFor(final String id) {
		return "name";
	}

	@Override
	public int allResultsSize() {
		return this.getUserService().sizeOfSearch(this.getSearchValue());

	}

	@Override
	public Iterator<? extends User> getResults(final String searchValue,
			final int first, final int count) {
		return this.getUserService().getResults(searchValue, first, count)
				.iterator();
	}

	@Override
	public void delete(final User user) {
		this.getLibraryServiceFacade().delete(user);
	}

	@Override
	protected void update(final User user) {
		this.getLibraryServiceFacade().update(user);
	}

	@Override
	@Transactional
	protected void save(final User user) {
		if (this.getPassword().equals(this.getPasswordConfirmation())) {
			this.getLibraryServiceFacade().add(user);
		} else {
			throw new ServiceException("No equal passwords");
		}
	}

	@Override
	protected User getNewEntity() {
		return new User();
	}

	@Override
	protected void setPrimaryKeyInNewEntity(final User entity,
			final String primaryKey) {
		entity.setName(primaryKey);
	}

}
